package com.hzqy.web.ac.itf;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.hzqy.commons.utils.SystemConfigUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzqy.commons.utils.IOUtil;
import com.hzqy.service.ift.ItfServiceBean;
import com.hzqy.service.wailian.WaiLianServiceBean;
import com.hzqy.web.vo.WlContentVo;

@Controller
@RequestMapping("/itf")
public class ItfAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Resource
	private ItfServiceBean itfServiceBean;
	
	@Resource
	private WaiLianServiceBean waiLianServiceBean ;

	/**
	 * 视频转码接口回调 提供给数通回调
	 */
	@ResponseBody
	@RequestMapping("/updateMediaCode")
	public Object updateMediaCode(HttpServletRequest request) {

		String url = SystemConfigUtils.getSysConfig("xy_url3");
		WlContentVo wlContentVo = new WlContentVo();
		wlContentVo.setF_wlc_path(url);
		wlContentVo.setSysConfigKey("control_link_ab");
		wlContentVo = waiLianServiceBean.selectWLContentAvaliable(wlContentVo);
		Map<String, Object> res = new HashMap<>();
		if(wlContentVo!=null){
			long t1 = System.currentTimeMillis();
			int flag = 0;
			Map<String, Object> resultMap = new HashMap<String, Object>();
			try {
				String json = IOUtil.getStringFromInputStream(request.getInputStream());
				json = URLDecoder.decode(json, "UTF-8");
				LOG.debug("(" + t1 + ") 视频转码接口回调: " + json);
				Map<String, Object> result = itfServiceBean.updateMediaCode(json);
				String id = String.valueOf(result.get("f_cc_id"));
				flag = Integer.valueOf(String.valueOf(result.get("flag")));
				resultMap.put("id", id);
			} catch (Exception e) {
				LOG.error("", e);
			}
			
			if(flag > 0) {
				resultMap.put("errorcode", 0);
				resultMap.put("errormsg","mediaCode获取成功");
			} else {
				resultMap.put("errorcode", -1);
				resultMap.put("errormsg","mediaCode获取失败!flag="+flag);
			}
			LOG.debug("(" + t1 + ") 视频转码接口回调结果: " + resultMap + " 处理耗时: " + (System.currentTimeMillis() - t1) + "ms");
			return resultMap;
		}else{
			res.put("msg", "接口已关闭");
			res.put("success", "0");
		}
		return res;
	}
	
	/**
	 * 获取视频转码进度 提供给翼校通调用
	 */
	@ResponseBody
	@RequestMapping("/getZmjdQueue")
	public Object getZmjdQueue(
			HttpServletRequest request,
			@RequestParam(value = "contentid") int contentid) {
		String url = SystemConfigUtils.getSysConfig("xy_url4");
		WlContentVo wlContentVo = new WlContentVo();
		wlContentVo.setF_wlc_path(url);
		wlContentVo.setSysConfigKey("control_link_ab");
		wlContentVo = waiLianServiceBean.selectWLContentAvaliable(wlContentVo);
		
		Map<String, Object> result = new HashMap<>();
		if(wlContentVo!=null){
			long t1 = System.currentTimeMillis();
			Map<String, Object> resultMap = new HashMap<String, Object>();
			LOG.debug("(" + t1 + ") 获取数通视频转码队列接口参数: contentid=" + contentid);
			result = itfServiceBean.getZmjdQueue(contentid);
			LOG.debug("(" + t1 + ") 获取数通视频转码队列接口结果: " + resultMap + " 处理耗时: " + (System.currentTimeMillis() - t1) + "ms");
		}else{
			result.put("msg", "接口已关闭");
			result.put("success", "0");
		}
		return result;
	}
	
	
	/**
	 * 校园直播开通数据新增接口 前端调用，将数据发送至直播方
	 */
	@ResponseBody
	@RequestMapping("/zbDataInsertInterface")
	public Object zbDataInterface(
			HttpServletRequest request,
			@RequestParam(value = "serverid") String serverid,
			@RequestParam(value = "orgId") int orgId) {
		String url = SystemConfigUtils.getSysConfig("xy_url1");
		WlContentVo wlContentVo = new WlContentVo();
		wlContentVo.setF_wlc_path(url);
		wlContentVo.setSysConfigKey("control_link_ab");
		wlContentVo = waiLianServiceBean.selectWLContentAvaliable(wlContentVo);
		Map<String, Object> result = new HashMap<>();
		
		if(wlContentVo!=null){
			long t1 = System.currentTimeMillis();
			LOG.debug("(" + t1 + ") 校园直播开通数据新增接口 serverid: " + serverid + " orgId: " + orgId);
			Map<String, Object> resultMap = itfServiceBean.zbDataInsertInterface(serverid, orgId);
			LOG.debug("(" + t1 + ") 校园直播开通数据新增接口结果: " + resultMap + " 处理耗时: " + (System.currentTimeMillis() - t1) + "ms");
			return resultMap;
		}else{
			result.put("msg", "接口已关闭");
			result.put("success", "0");
		}
		return result;
	}
	
	/**
	 * 校园直播开通数据修改接口 前端调用，将数据发送至直播方
	 */
	@ResponseBody
	@RequestMapping("/zbDataUpdateInterface")
	public Object zbDataUpdatenterface(
			HttpServletRequest request,
			@RequestParam(value = "serverid") String serverid,
			@RequestParam(value = "orgId") int orgId) {
		String url = SystemConfigUtils.getSysConfig("xy_url");
		WlContentVo wlContentVo = new WlContentVo();
		wlContentVo.setF_wlc_path(url);
		wlContentVo.setSysConfigKey("control_link_ab");
		wlContentVo = waiLianServiceBean.selectWLContentAvaliable(wlContentVo);
		Map<String, Object> result = new HashMap<>();
		
		if(wlContentVo!=null){
			long t1 = System.currentTimeMillis();
			LOG.debug("(" + t1 + ") 校园直播开通数据修改接口 serverid: " + serverid + " orgId: " + orgId);
			Map<String, Object> resultMap = itfServiceBean.zbDataUpdatenterface(serverid, orgId);
			LOG.debug("(" + t1 + ") 校园直播开通数据修改接口结果: " + resultMap + " 处理耗时: " + (System.currentTimeMillis() - t1) + "ms");
			return resultMap;
		}else{
			result.put("msg", "接口已关闭");
			result.put("success", "0");
		}
		return result;
	}
	
	
	/**
	 * 校园直播审核结果回调 提供给直播方回调
	 */
	@ResponseBody
	@RequestMapping("/zbMessageBack")
	public Object zbMessageBack(
			HttpServletRequest request,
			@RequestParam(value = "result") String result) {
		String url = SystemConfigUtils.getSysConfig("xy_url2");
		WlContentVo wlContentVo = new WlContentVo();
		wlContentVo.setF_wlc_path(url);
		wlContentVo.setSysConfigKey("control_link_ab");
		wlContentVo = waiLianServiceBean.selectWLContentAvaliable(wlContentVo);
		Map<String, Object> res = new HashMap<>();
		
		if(wlContentVo!=null){
			long t1 = System.currentTimeMillis();
			LOG.debug("(" + t1 + ") 校园直播审核结果回调");
			Map<String, Object> resultMap = itfServiceBean.zbMessageBack(result);
			LOG.debug("(" + t1 + ") 校园直播审核结果回调结果: " + resultMap + " 处理耗时: " + (System.currentTimeMillis() - t1) + "ms");
			return resultMap;
		}else{
			res.put("msg", "接口已关闭");
			res.put("success", "0");
		}
		return res;
	}
	
	
}
